<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var $ = (function () {
      var xhr = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHttp');
      if (!xhr) {
        throw new Error('您的浏览器不支持异步发起HTTP请求');
      }
      function _doAjax(opt) {
        var opt = opt || {},
            type = (opt.type || 'GET').toUpperCase(),
            async = opt.async || true,
            url= opt.url,
            data = opt.data || null,
            error = opt.error || function(){},
            success = opt.success || function(){},
            complete = opt.complete || function(){};
           if(!url){
             throw new Error('您没有填写URL');
           } 
           xhr.open(type,url,async);
           type === 'POST' && xhr.setRequestHeader('Content-type','application/json');
           xhr.send(type === 'GET'? null : jsonDatas(data));//发送请求
           xhr.onreadystatechange = function (){
             if(xhr.readyState === 4 && xhr.status ===200){
               success(JSON.parse(xhr.responseText));
             }
             if(xhr.status === 404){
               error();
             }
             complete();
           }
      }
      function formDatas(obj) {
        var str = '';
        for(var key in obj){
          str = key + '=' + obj[key] + '&';
        }
        return str.replace(/&$/,'');
      }

      function jsonDatas(obj) {
        console.log(JSON.stringify(obj));
        return JSON.stringify(obj)
      }

      return {
        ajax: function (opt) {
          _doAjax(opt);
        },
        post: function () {

        },
        get: function () {

        }
      }
    })();

    $.ajax({
      type:'post',
      url:'http://localhost:8081/login',
      data:{
        mobile:'213912345678',
        code:'123456'
      },
      success: function(data){
        console.log(data);
      }
    })
  </script>
</body>

</html>